package org.jetnuke.modules.news;

import org.apache.log4j.Logger;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.dao.StoriesDAO;
import org.jetnuke.entities.Stories;
import org.jetnuke.entities.StoryCheck;
import org.jetnuke.util.JetUtil;

public class RateArticle implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(RateArticle.class);

	int id = -1, score;

	public void setId(int id) {
		this.id = id;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (id != -1) {
			if (score > 5) {
				score = 5;
			}
			if (score < 1) {
				score = 1;
			}

			GregorianCalendar gc = new GregorianCalendar();
			gc.add(Calendar.MINUTE, -30);

			Date dt1 = gc.getTime();
			/*
			 * Iterator it = story_checkPeer.doSelect( new
			 * Criteria().add(story_checkPeer.TIME, dt1,
			 * Criteria.LESS_EQUAL)).iterator(); while (it.hasNext()) {
			 * story_check check = (story_check) it.next();
			 * story_checkPeer.doDelete(check); }
			 */

			DataAccessDriver.getInst().newStoryCheckDAO().deleteTimeLessThen(
					dt1);

			String ip = request.getRemoteAddr();
			List found = DataAccessDriver.getInst().newStoryCheckDAO()
					.selectByIP_STORY(ip, id);

			// story_checkPeer.doSelect(new Criteria().add(
			// story_checkPeer.IP, ip).add(story_checkPeer.STORY_ID, id)
			// .setLimit(1));
			boolean valid = true;
			if (!found.isEmpty()) {
				valid = false;
			} else {
				StoryCheck check = new StoryCheck();
				check.setSTORY_ID(id);
				check.setIP(ip);
				check.setTIME(new Date());
				DataAccessDriver.getInst().newStoryCheckDAO().addNew(check);
				// check.save();
			}
			if (valid) {
				StoriesDAO sm = DataAccessDriver.getInst().newStoriesDAO();
				Stories story = sm.retrieveByPK(id);

				story.setRATINGS(story.getRATINGS() + 1);
				story.setSCORE(story.getSCORE() + score);
				sm.update(story);
			}
		}
		JetUtil.redirect(vc, response, request.getContextPath()
				+ "/do/news/story?id=" + id);

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

	public void setScore(int score) {
		this.score = score;
	}
}
